<?php 
defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE.'/'.$module.'/common.inc.php';
$MG['guide_limit'] > -1 or Prompt::alert(lang('message->without_permission_and_upgrade'), 'goback');

require_once DT_INC.'/post.func.php';
include load($module.'.lang');
include load('my.lang');
$do = new GuideModule($moduleid);

$sql = $_userid ? "username='$_username'" : "ip='$DT_IP'";
$limit_used = $limit_free = $need_password = $need_captcha = $need_question = $fee_add = 0;
if(in_array($action, array('', 'add'))) {
	$r = $db->getOne("SELECT COUNT(*) AS num FROM {$table} WHERE $sql AND status>1");
	$limit_used = $r['num'];
	$limit_free = $MG['quote_limit'] > $limit_used ? $MG['quote_limit'] - $limit_used : 0;
}

$suffix = '';
if($com)$suffix .='&com='.$com;
switch($action) {
	case 'add':
		$userinfo = userinfo($_username);
		if($MG['guide_limit'] && $limit_used >= $MG['guide_limit']) Prompt::alert(lang($L['info_limit'], array($MG[$MOD['module'].'_limit'], $limit_used)), $_userid ? url('member/my').'?mid='.$mid : url('member/my'));
		if($MG['day_limit']) {
			$today = $today_endtime - 86400;
			$r = $db->getOne("SELECT COUNT(*) AS num FROM {$table} WHERE $sql AND addtime>$today");
			if($r && $r['num'] >= $MG['day_limit']) Prompt::alert(lang($L['day_limit'], array($MG['day_limit'])), $_userid ? url('member/my').'?mid='.$mid : url('member/my'));
		}

		if($MG['guide_free_limit'] >= 0) {
			$fee_add = ($MOD['fee_add'] && (!$MOD['fee_mode'] || !$MG['fee_mode']) && $limit_used >= $MG['guide_free_limit'] && $_userid) ? dround($MOD['fee_add']) : 0;
		} else {
			$fee_add = 0;
		}
		$fee_currency = $MOD['fee_currency'];
		$fee_unit = $fee_currency == 'money' ? $DT['money_unit'] : $DT['credit_unit'];
		$need_password = $fee_add && $fee_currency == 'money';
		$need_captcha = $MOD['captcha_add'] == 2 ? $MG['captcha'] : $MOD['captcha_add'];
		$need_question = $MOD['question_add'] == 2 ? $MG['question'] : $MOD['question_add'];

		if($post) {

			//if($fee_add && $fee_add > ($fee_currency == 'money' ? $_money : $_credit)) Prompt::alert($L['balance_lack']);
			//if($need_password && !is_payword($_username, $password)) Prompt::alert($L['error_payword']);
			if($MG['add_limit']) {
				//$last = $db->getOne("SELECT addtime FROM {$table} WHERE $sql ORDER BY itemid DESC");
				//if($last && $DT_TIME - $last['addtime'] < $MG['add_limit']) Prompt::alert(lang($L['add_limit'], array($MG['add_limit'])));
			}
			$msg = captcha($captcha, $need_captcha, true);
			if($msg) Prompt::alert($msg);
			$msg = question($answer, $need_question, true);
			if($msg) Prompt::alert($msg);
			if(isset($post['islink'])) unset($post['islink']);
			$post['clear_link'] = $MOD['clear_link'];
			if($do->pass($post)) {

				$post['city'] = explode(',',$post['city']);
				$return = push_charge($_groupid,$userinfo,$post['city'],$moduleid);
				if(!$return['success'] && $return['total'] > 0){
					$ajaxReturn =array('success'=>$return['success'],'total'=>$return['total']);
					exit(json_encode($ajaxReturn));
				};
				$post['total'] = $return['total'];
				$CAT = get_cat($post['catid']);
				if(!$CAT || !check_group($_groupid, $CAT['group_add'])) Prompt::alert(lang($L['group_add'], array($CAT['catname'])));
				$post['addtime'] = $post['level'] = $post['fee'] = 0;
				$post['style'] = $post['template'] = $post['note'] = $post['filepath'] = '';
				$need_check =  $MOD['check_add'] == 2 ? $MG['check'] : $MOD['check_add'];
				$post['status'] = get_status(3, $need_check);
				$post['hits'] = 0;
				$post['username'] = $_username;
				$post['save_remotepic'] = $MOD['save_remotepic'] ? 1 : 0;
				$post['clear_link'] = $MOD['clear_link'] ? 1 : 0;
				$post['introduce_length'] = $MOD['introduce_length'] ? $MOD['introduce_length'] : 0;
				$do->add($post);
				$msg = $post['status'] == 2 ? $L['success_check'] : $L['success_add'];
				$js = '';

				if($_userid) {
					Cookie::set('dmsg', $msg);
					$forward = url('member/my').'?mid='.$mid.'&status='.$post['status'].$suffix;
					$msg = '信息发布成功';
				} else {
					$forward = url('member/my').'?mid='.$mid.'&action=add';
				}
				$js .= 'window.onload=function(){parent.window.location="'.$forward.'";}';
				Prompt::alert($msg, '', $js);
			} else {
				Prompt::alert($do->getErrmsg(), '', ($need_captcha ? reload_captcha() : '').($need_question ? reload_question() : ''));
			}
		} else {
			foreach($do->getFields() as $v) {
				$$v = '';
			}
			$content = '';
			$catid = 0;
			$areaid = $cityid;
			$item = array();
		}
	break;
	case 'edit':
		$itemid or Prompt::message();
		$do->setItemid($itemid);
		$item = $do->getOne();
		if(!$item || $item['username'] != $_username) Prompt::message();

		if($MG['edit_limit'] < 0) Prompt::message($L['edit_refuse']);
		if($MG['edit_limit'] && $DT_TIME - $item['addtime'] > $MG['edit_limit']*86400) Prompt::message(lang($L['edit_limit'], array($MG['edit_limit'])));

		if($post) {
			if($item['islink']) {
				$post['islink'] = 1;
			} else if(isset($post['islink'])) {
				unset($post['islink']);
			}
			$post['clear_link'] = $MOD['clear_link'];
			if($do->pass($post)) {
				$CAT = get_cat($post['catid']);
				if(!$CAT || !check_group($_groupid, $CAT['group_add'])) Prompt::alert(lang($L['group_add'], array($CAT['catname'])));
				$post['addtime'] = timetodate($item['addtime']);
				$post['level'] = $item['level'];
				$post['fee'] = $item['fee'];
				$post['style'] = $item['style'];
				$post['template'] = $item['template'];
				$post['filepath'] = $item['filepath'];
				$post['note'] = $item['note'];
				$need_check =  $MOD['check_add'] == 2 ? $MG['check'] : $MOD['check_add'];
				$post['status'] = get_status($item['status'], $need_check);
				$post['hits'] = $item['hits'];
				$post['save_remotepic'] = $MOD['save_remotepic'] ? 1 : 0;
				$post['clear_link'] = $MOD['clear_link'] ? 1 : 0;
				$post['get_introduce'] = $MOD['get_introduce'] ? 1 : 0;
				$post['introduce_length'] = $MOD['introduce_length'] ? $MOD['introduce_length'] : 0;

				$do->edit($post);
				Cookie::set('dmsg', $L['success_edit']);
				Prompt::alert('', '', 'parent.window.location="'.$forward.'"');
			} else {
				Prompt::alert($do->getErrmsg());
				$city = trim($city,'|');
				$city = explode('|',$city);
			}
		} else {
			extract($item);
			$city = trim($city,'|');
			$city = explode('|',$city);
		}
	break;
	case 'delete':
		$MG['delete'] or Prompt::message();
		$itemid or Prompt::message();
		$itemids = is_array($itemid) ? $itemid : array($itemid);
		foreach($itemids as $itemid) {
			$do->setItemid($itemid);
			$item = $do->getOne();
			if(!$item || $item['username'] != $_username) Prompt::message();
			$do->recycle($itemid);
		}
		dmsg($L['success_delete'], $forward);
	break;
	default:
		$status = isset($status) ? intval($status) : 3;
		in_array($status, array(1, 2, 3)) or $status = 3;
		$condition = "username='$_username'";
		$condition .= " AND status=$status";
		if($keyword) $condition .= " AND keyword LIKE '%$keyword%'";
		if($catid) $condition .= ($CAT['child']) ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
		$timetype = strpos($MOD['order'], 'edit') === false ? 'add' : '';
		$lists = $do->getList($condition, $MOD['order']);
		break;
}

$head_title = lang($L['module_manage'], array($MOD['name']));

if($_userid) {
	$nums = array();
	for($i = 1; $i < 5; $i++) {
		$r = $db->getOne("SELECT COUNT(*) AS num FROM {$table} WHERE username='$_username' AND status=$i");
		$nums[$i] = $r['num'] ? $r['num'] : 0;
	}

}
include template('my_'.$module, 'member');